package org.xxx.epms.gateway.security.filter;

import cn.hutool.core.text.AntPathMatcher;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import org.xxx.epms.common.utils.JwtTokenUtil;
import reactor.core.publisher.Mono;

/**
 * JwtAuthenticationFilter
 * Description: Jwt登录认证过滤器
 *
 * @author lb
 */

@Slf4j
public class JwtAuthenticationFilter implements WebFilter {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    private final String TOKEN_HEADER = "Authorization";
    private final String TOKEN_HEAD = "Bearer ";

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        AntPathMatcher antPathMatcher = new AntPathMatcher();

        if(!antPathMatcher.match("/admin/login", request.getURI().getPath())&&!antPathMatcher.match("/admin/register", request.getURI().getPath()) &&!antPathMatcher.match("/inspector/login", request.getURI().getPath()) &&!antPathMatcher.match("/inspector/register", request.getURI().getPath())){
            String authHeader = request.getHeaders().getFirst(TOKEN_HEADER);
            if (authHeader != null && authHeader.startsWith(TOKEN_HEAD)) {
                String token = authHeader.substring(TOKEN_HEAD.length());
                String username = jwtTokenUtil.getUserNameFromToken(token);
                if(username == null) {
                    JSONObject jsonObject = setResultErrorMsg(401,"token无效");
                    DataBuffer buffer = response.bufferFactory().wrap(jsonObject.toJSONString().getBytes());
                    return response.writeWith(Mono.just(buffer));
                }
                if(jwtTokenUtil.isTokenExpired(token)){
                    JSONObject jsonObject = setResultErrorMsg(401,"token已过期");
                    DataBuffer buffer = response.bufferFactory().wrap(jsonObject.toJSONString().getBytes());
                    return response.writeWith(Mono.just(buffer));
                }
                return chain.filter(exchange);
            }
            JSONObject jsonObject = setResultErrorMsg(500,"未登录");
            DataBuffer buffer = response.bufferFactory().wrap(jsonObject.toJSONString().getBytes());
            return response.writeWith(Mono.just(buffer));
        }
        return chain.filter(exchange);
    }

    private JSONObject setResultErrorMsg(Integer code,String msg) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);
        jsonObject.put("message", msg);
        return jsonObject;
    }
}
